<?php

/**
*
*/
define('IN_SMART', true);
$smt_root_path = (defined('SMT_ROOT_PATH')) ? SMT_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($smt_root_path . 'common.' . $phpEx);

$user->session_begin();

if (!$user->data['is_registered'])
{
	login_box('', 'Login');
	return true;
}

$mode			= request_var('mode', '');
$story_id		= request_var('story_id', 0);
$channel_id	= request_var('channel_id', 0);
$story_link	= request_var('story_link', '');
$story_time		= request_var('story_time', '');
$story_title 	= utf8_normalize_nfc(request_var('story_title', '', true));
$story_author	= utf8_normalize_nfc(request_var('story_author', '', true));
$story_blurb	= utf8_normalize_nfc(request_var('story_blurb', '', true));
$story_status	= isset($_POST['story_status'])? 1 : 0;

$usr_message = '';

switch ($mode)
{
	case 'delete':
		if ($story_id != 0 && $channel_id != 0){
			
			$sql = 'UPDATE ' . STORIES_TABLE . ' 
				SET story_status = 1 
				WHERE story_id = ' . $story_id;
			$result = $db->sql_query($sql);

			header('Refresh: 0; URL=viewchannel.php?channel_id=' . $channel_id);

		} else {
			$usr_message .= 'No article is specified<br />';
		}

	break;

	case 'submit':

		//validate
		if ($story_title == '' || $story_link == '' || $story_author == '' || $story_time == '' || $story_blurb == '') {
			$usr_message .= 'Some fields are empty, please check your input again<br />';
			$story_data['mode'] = 'submit';
			$story_data['story_id'] = $story_id;
			$story_data['channel_id'] = $channel_id;
			$story_data['story_link'] = $story_link;
			$story_data['story_time'] = $story_time;
			$story_data['story_title'] = $story_title;
			$story_data['story_author'] = $story_author;
			$story_data['story_blurb'] = $story_blurb;
			$story_data['story_status_checked'] = ($story_status == 0)? '' : ' checked';

			if ($channel_id != 0){
				$sql = 'SELECT 
						channel_id, 
						channel_name
					FROM ' . CHANNELS_TABLE . ' 
					WHERE channel_id = ' . $channel_id;
				$result = $db->sql_query($sql);
				$channel_data = $db->sql_fetchrow($result);
				$story_data['channel_name'] = $channel_data['channel_name'];
				$db->sql_freeresult($result);
			} else {
				$usr_message .= 'No channel is specified<br />';
			}

		} else {
			if ($story_id != 0){

			$sql = 'UPDATE ' . STORIES_TABLE . ' SET ' . $db->sql_build_array('UPDATE', array(
				'story_title'		=> $story_title,
				'story_link'		=> $story_link,
				'story_author'		=> $story_author,
				'story_time'		=> $story_time,
				'story_blurb'	=> $story_blurb,
				'story_status'	=> $story_status)
			) . ' WHERE story_id = ' . $story_id;
			$db->sql_query($sql);

			} else {

				$sql = 'INSERT INTO ' . STORIES_TABLE . ' ' . $db->sql_build_array('INSERT', array(
					'channel_id'		=> $channel_id,
					'story_title'		=> $story_title,
					'story_link'		=> $story_link,
					'story_author'		=> $story_author,
					'story_time'	=> $story_time,
					'story_blurb'	=> $story_blurb,
					'story_status'	=> $story_status)
				); 
				$db->sql_query($sql);
				
				$sql = 'UPDATE ' . CHANNELS_TABLE . ' 
					SET channel_stories = channel_stories + 1
					WHERE channel_id = ' . $channel_id;
				$db->sql_query($sql);

			}
			header('Refresh: 0; URL=viewchannel.php?channel_id=' . $channel_id);
		}

	break;
	
	case 'edit':
		if ($story_id != 0){
			$sql = 'SELECT 
					s.story_id, 
					s.channel_id, 
					s.story_title, 
					s.story_link, 
					s.story_blurb,
					s.story_author,
					s.story_time,
					s.story_status,
					c.channel_name
				FROM ' . STORIES_TABLE . ' s
				LEFT JOIN ' . CHANNELS_TABLE . ' c
					ON (
						s.channel_id = c.channel_id
					)
				WHERE s.story_id = ' . $story_id ;
			$result = $db->sql_query($sql);
			$story_data = $db->sql_fetchrow($result);
			$db->sql_freeresult($result);

			$story_data['story_gmdate'] = gmdate('Y-m-d H:i:s O', $story_data['story_time']);
			$story_data['story_status_checked'] = ($story_data['story_status'] == 0)? '' : ' checked';
			$story_data['mode'] = 'submit';
		} else {
			$usr_message .= 'No article is specified<br />';
		}

	break;

	case 'post':
	default: 
		if ($channel_id != 0){
			$sql = 'SELECT 
					channel_id, 
					channel_name
				FROM ' . CHANNELS_TABLE . ' 
				WHERE channel_id = ' . $channel_id;
			$result = $db->sql_query($sql);
			$story_data = $db->sql_fetchrow($result);
			$db->sql_freeresult($result);
		} else {
			$usr_message .= 'No channel is specified<br />';
		}
		$story_data['story_time'] = time();
		$story_data['story_gmdate'] = gmdate('Y-m-d H:i:s O', $story_data['story_time']);
		$story_data['mode'] = 'submit';
	break;
}


/*
$sql = 'SELECT 
		channel_id, 
		channel_name
	FROM ' . CHANNELS_TABLE . ' 
	ORDER BY channel_id ASC';

$result = $db->sql_query($sql);

$stories = array();
while ($row = $db->sql_fetchrow($result))
{
	
	$channels[] = $row;
}
$db->sql_freeresult($result);
*/

$template->assign('msg_box', $usr_message);
$template->assign('story_data', $story_data);


$template->assign('config', $config);
$template->assign('name', $user->data['username']);

page_header('Post/Edit Panel');

$tpl_body = 'posting_body.html';
page_footer();

?>